<template>
  <div class="home">
    <!-- 搜索框 -->
    <van-nav-bar id="page-nav-bar" fixed>
      <van-button class="search-btn" slot="title" icon="search" size="mini" round color="#3caaac" to="/search"
      >搜索</van-button>
    </van-nav-bar>

    <!-- 标签栏 -->
    <van-tabs class="channel-tabs" animated swipeable v-model="cur">
      <van-tab v-for="item in channelList" :key="item.id" :title="item.name">
        <!-- 文字列表组件 -->
        <text-list :channel="item"></text-list>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="hamburger-btn" @click="channelEditShow = true">
        <i class="iconfont icon-gengduo1"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑对话框 -->
    <van-popup
      v-model="channelEditShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <channelEdit :myChannel="channelList" :cur="cur" @changeCur="changeCur" />
    </van-popup>
  </div>
</template>

<script>
import { channelDataList } from '@/api/channel'
import textList from './component/textList.vue'
import channelEdit from '@/components/channel-edit/index.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  data () {
    return {
      channelList: [],
      // 控制频道编辑对话框展示
      channelEditShow: false,
      cur: 0 // 当前选择频道
    }
  },
  created () {
    this.getChannelList()
  },
  watch: {
    cur (newval) {
      console.log(newval)
    }
  },
  methods: {
    async getChannelList () {
      // 用户已登录 获取后端数据
      if (this.user) {
        try {
          const { data: res } = await channelDataList()
          // console.log(res, 999)
          this.channelList = res.data.channels
        } catch (err) {
          this.$toast('获取频道列表数据失败')
        }
      } else {
        // 用户未登录 有本地存储数据
        const channels = getItem('TOUTIAO_CHANNELS')
        if (channels) {
          this.channelList = channels
        } else {
          // 用户未登录 无本地存储数据
          try {
            const { data: res } = await channelDataList()
            this.channelList = res.data.channels
          } catch (err) {
            this.$toast('获取频道列表数据失败')
          }
        }
      }
    },
    changeCur (index, isShow = true) {
      // console.log(index, 222)
      this.channelEditShow = isShow
      this.cur = index
    }
  },
  components: {
    textList,
    channelEdit
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.home {
  padding:174px 0 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
    .search-btn {
      width: 555px;
      height: 64px;
      font-size: 28px;
      border: none;
      .van-icon {
        font-size: 32px;
      }
    }
  }
  /deep/ .van-hairline--bottom::after  {
    border: none;
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top:92px;
      left: 0;
      right: 0;
      z-index: 999;
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: teal;
    }
    .placeholder {
      flex-shrink: 0;
      width: 50px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background: #fff;
      opacity: 0.902;
      .iconfont {
        font-size: 33px;
      }
      &::before {
        content: "";
        position: absolute;
        left: 0;
        width: 2px;
        height: 100%;
        background-image: url(~@/assets/img/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
